var TChannel = require('tchannel');
 
var server = new TChannel();
var client = new TChannel();
 
var serverChan = server.makeSubChannel({
    serviceName: 'server2'
});


// normal response 
serverChan.register('func1', function onReq(req, res, arg2, arg3) {
    console.log('server2 func1 responding', { arg2: arg2.toString(), arg3: arg3.toString() });
    res.headers.as = 'raw';
    res.sendOk('result', 'indeed it did');
});
 

// err response 
serverChan.register('func2', function onReq2(req, res) {
    res.headers.as = 'raw';
    res.sendNotOk(null, 'it failed');
});
 
server.listen(4040, '127.0.0.1', function onListen() {
    console.log([server.hostPort]);
    // var clientChan = client.makeSubChannel({
    //     serviceName: 'server',
    //     peers: [server.hostPort],
    //     requestDefaults: {
    //         hasNoParent: true,
    //         headers: { 'as': 'raw', 'cn': 'example-client' }
    //     }
    // });
 
    // clientChan.request({
    //     serviceName: 'server',
    //     timeout: 1000
    // }).send('func1', 'arg 1', 'arg 2', function onResp(err, res, arg2, arg3) {
    //     console.log('normal res:', { arg2: arg2.toString(), arg3: arg3.toString() });
    // });
 
    // clientChan.request({
    //     serviceName: 'server'
    // }).send('func2', 'arg 1', 'arg 2', function onResp(err, res, arg2, arg3) {
    //     console.log('err res: ', { ok: res.ok, message: String(arg3) });
    // });
});